package com.jiaxianm.secure.config;

import com.jiaxianm.secure.filter.DecryptReplaceStreamFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * @ClassName : WebAllHandlerConfig
 * @Description : 过滤器、监听器、拦截器 前置处理配置类
 * @Author : AD
 */

@Configuration
public class WebAllHandlerConfig {

    /**
     * 注册过滤器
     * */
    @Bean
    public FilterRegistrationBean AllFilterRegistration(){
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter(decryptReplaceStreamFilter());
        registration.addUrlPatterns("/*");
        registration.setName("APISecurityFilter");
        //需要注意解密过滤器Filter 的优先级一点要设置为最高优先级registration.setOrder(1);
        // 首先需要该过滤器对加密数据进行解密后在重新封装请求，才能使后续的业务数据能直接获取到解密后的数据，达到无感知的效果。
        registration.setOrder(1);
        return registration;
    }

    @Bean(name = "decryptReplaceStreamFilter")
    public Filter decryptReplaceStreamFilter(){
        return new DecryptReplaceStreamFilter();
    }


}

